package com.sskj.mine.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.RechargeListBean;
import com.sskj.mine.presenter.AssetRecordFragmentPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.MINE_FRAGMENT_ASSET_RECORD)//冲提币记录
public class AssetRecordFragment extends BaseFragment<AssetRecordFragmentPresenter> {
    @Autowired
    String code;
    @Autowired
    String id;
    @Autowired
    String type;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCCoolHelper<List<RechargeListBean>> mvcCoolHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment_asset_record;
    }

    @Override
    public AssetRecordFragmentPresenter getPresenter() {
        return new AssetRecordFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setLastDraw(false)
                .setSecondToLastDraw(false)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 40))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 40))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.mine_525869))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2)));

        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_asset_record, new SlimInjector<RechargeListBean>() {
            @Override
            public void onInject(RechargeListBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvAddressTitle, type.equals("recharge") ? App.INSTANCE.getString(R.string.mineAssetRecordFragment1) : App.INSTANCE.getString(R.string.mineAssetRecordFragment2))
                        .text(R.id.tvNumTitle, type.equals("recharge") ? App.INSTANCE.getString(R.string.mineAssetRecordFragment4) : App.INSTANCE.getString(R.string.mineAssetRecordFragment5))
                        .visibility(R.id.ll_check_time, type.equals("recharge") ? View.GONE : View.VISIBLE)
                        .visibility(R.id.tv_fail_tibi_msg, type.equals("recharge") ? View.GONE
                                : TextUtils.isEmpty(data.getMemo()) ? View.GONE
                                : data.getState().equals("3") || data.getState().equals("5") ? View.VISIBLE : View.GONE)
                        .text(R.id.tvSubmitTimeTitle, App.INSTANCE.getString(R.string.mineAssetRecordFragment6))
                        .text(R.id.tvCheckTimeTitle, App.INSTANCE.getString(R.string.mineAssetRecordFragment7))
                        .text(R.id.tvAddress, type.equals("recharge") ? data.getChongzhi_url() : data.getQianbao_url())
                        .text(R.id.tvNum, data.getPrice() + code)
                        .text(R.id.tvSubmitTime, TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(data.getAddtime()) * 1000))))
                        .text(R.id.tvCheckTime, TextUtils.isEmpty(data.getCheck_time()) ? "--" : TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(data.getCheck_time()) * 1000))))
                        .text(R.id.tv_fail_tibi_msg, data.getMemo())
                        .text(R.id.tvStatus, type.equals("recharge") ?
                                data.getState().equals("1") ? App.INSTANCE.getString(R.string.mine_recy_item_asset_record79)
                                        : App.INSTANCE.getString(R.string.mine_recy_item_asset_record78)
                                : data.getState().equals("1") ? App.INSTANCE.getString(R.string.mine_recy_item_asset_record80)
                                : data.getState().equals("2") ? App.INSTANCE.getString(R.string.mine_recy_item_asset_record81)
                                : data.getState().equals("3") ? App.INSTANCE.getString(R.string.mine_recy_item_asset_record82)
                                : data.getState().equals("4") ? App.INSTANCE.getString(R.string.mine_recy_item_asset_record83)
                                : App.INSTANCE.getString(R.string.mine_recy_item_asset_record84))
                        .textColor(R.id.tvStatus, getResources().getColor(data.getState().equals("3") || data.getState().equals("5") ? R.color.mine_ff4 : R.color.commonWhite));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<RechargeListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List<RechargeListBean>> loadSource(int page) {
                return mPresenter.getAssetRecord(page, 10, type, id);
            }
        }, 10);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
    }
}
